﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SistemaAcademicoBO;
using System.Data;
using System.Configuration;
using SistemaAcademicoBO.Entities;

namespace SistemaAcademicoDAL
{
    public class UserDAO : AbstratcDAO, IDAO<IEntity>
    {
        public UserDAO()
        {
            connection = new Connection();
        }

        public IEntity GetRecord<K>(K id)
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();

            ProcedureCall procedureCall = new ProcedureCall("pr_retrieveUser", connection.GetSQLConnection());
            procedureCall.Parameters.Add(new ProcedureParam("@UserId", SqlDbType.Int, 0, id));

            procedureCall.Execute(true);

            List<IEntity> resultSet = procedureCall.ExtractFromResultSet(typeof(User));

            connection.CloseConnection();

            if (resultSet.Count != 1)
                return null;

            return resultSet[0];
        }

        public IEntity GetRecordByUserLogin<K>(K id)
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();

            ProcedureCall procedureCall = new ProcedureCall("pr_retrieveUserByUserLogin", connection.GetSQLConnection());
            procedureCall.Parameters.Add(new ProcedureParam("@UserLogin", SqlDbType.VarChar, 100, id));

            procedureCall.Execute(true);

            List<IEntity> resultSet = procedureCall.ExtractFromResultSet(typeof(User));

            connection.CloseConnection();

            if (resultSet.Count != 1)
                return null;

            return resultSet[0];
        }

        public List<IEntity> GetAllRecords()
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();

            ProcedureCall procedureCall = new ProcedureCall("pr_retrieveAllUsers", connection.GetSQLConnection());

            procedureCall.Execute(true);

            List<IEntity> resultSet = procedureCall.ExtractFromResultSet(typeof(User));

            connection.CloseConnection();

            return resultSet;
        }

        public void SetRecord(IEntity entity)
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();
            ProcedureCall procedureCall = new ProcedureCall("pr_storeUser", connection.GetSQLConnection());

            User user = (User)entity;
            procedureCall.Parameters.Add(new ProcedureParam("@UserId", SqlDbType.Int, 0, user.UserId));
            procedureCall.Parameters.Add(new ProcedureParam("@UserLogin", SqlDbType.VarChar, 50, user.UserLogin));
            procedureCall.Parameters.Add(new ProcedureParam("@UserPassword", SqlDbType.VarChar, 100, user.UserPassword));
            procedureCall.Parameters.Add(new ProcedureParam("@UserAlcada", SqlDbType.Int, 0, user.UserAlcada));
            procedureCall.Parameters.Add(new ProcedureParam("@ReferenceId", SqlDbType.VarChar, 200, user.ReferenceId));

            procedureCall.Execute(false);

            connection.CloseConnection();
        }

        public void RemoveRecord<K>(K id)
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();

            ProcedureCall procedureCall = new ProcedureCall("pr_removeUser", connection.GetSQLConnection());

            procedureCall.Parameters.Add(new ProcedureParam("@UserId", SqlDbType.Int, 0, id));

            procedureCall.Execute(false);

            connection.CloseConnection();
        }
    }
}
